package org.jeecg.glr.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.glr.entity.GlrEquipmentStatusLog;
import org.jeecg.glr.entity.GlrGateway;
import org.jeecg.glr.service.IGlrEquipmentStatusLogService;
import org.jeecg.glr.service.IGlrGatewayService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 设备状态日志
 * @Author: jeecg-boot
 * @Date:   2020-03-26
 * @Version: V1.0
 */
@RestController
@RequestMapping("/glr/glrEquipmentStatusLog")
@Slf4j
public class GlrEquipmentStatusLogController extends JeecgController<GlrEquipmentStatusLog, IGlrEquipmentStatusLogService> {
	@Autowired
	private IGlrEquipmentStatusLogService glrEquipmentStatusLogService;
	@Autowired
	private IGlrGatewayService glrGatewayService;

	/**
	 * 分页列表查询
	 *
	 * @param glrEquipmentStatusLog
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GlrEquipmentStatusLog glrEquipmentStatusLog,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GlrEquipmentStatusLog> queryWrapper = QueryGenerator.initQueryWrapper(glrEquipmentStatusLog, req.getParameterMap());
		queryWrapper.isNotNull("type");
		Page<GlrEquipmentStatusLog> page = new Page<GlrEquipmentStatusLog>(pageNo, pageSize);
		IPage<GlrEquipmentStatusLog> pageList = glrEquipmentStatusLogService.page(page, queryWrapper);
		List<GlrEquipmentStatusLog> list=pageList.getRecords();
		for (GlrEquipmentStatusLog lg :
				list) {
			if(StringUtil.isNotEmpty(lg.getType())&&lg.getType().equals(GLRConstant.GLR_LIGHT_POLE_DEVICE_04)){
				GlrGateway glr= glrGatewayService.getGlrGatewayByCode(lg.getEquipmentCode());
				if(glr!=null){
					lg.setEquipmentName(glr.getGatewayName());
				}
			}
		}
		return Result.ok(pageList);
	}

	/**
	 *   添加
	 *
	 * @param glrEquipmentStatusLog
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GlrEquipmentStatusLog glrEquipmentStatusLog) {
		glrEquipmentStatusLogService.save(glrEquipmentStatusLog);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param glrEquipmentStatusLog
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GlrEquipmentStatusLog glrEquipmentStatusLog) {
		glrEquipmentStatusLogService.updateById(glrEquipmentStatusLog);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		glrEquipmentStatusLogService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.glrEquipmentStatusLogService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GlrEquipmentStatusLog glrEquipmentStatusLog = glrEquipmentStatusLogService.getById(id);
		if(glrEquipmentStatusLog==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(glrEquipmentStatusLog);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param glrEquipmentStatusLog
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GlrEquipmentStatusLog glrEquipmentStatusLog) {
        return super.exportXls(request, glrEquipmentStatusLog, GlrEquipmentStatusLog.class, "设备状态日志");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GlrEquipmentStatusLog.class);
    }

}
